/*
 * @Author: hrg
 * @Date: 2024-01-31 16:08:52
 * @LastEditTime: 2024-01-31 17:30:00
 * @FilePath: \study_go\study09_gin\gin_dome_01\main.go
 * @Description:  初识cookie
 */
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	// 创建路由
	r := gin.New()

	// 服务端要给客户端cookie
	r.GET("/cookie", cookieFunc)
	r.GET("/test", test)

	r.Run(":9090")

}

func cookieFunc(c *gin.Context) {
	// 获取客户端是否携带token
	cookie, err := c.Cookie("key_cookie")

	if err != nil {
		cookie = "NotSet"

		c.SetCookie("key_cookie", "value_cookie", 60, "/",
			"localhost", false, true)
	}
	fmt.Printf("cookie的值是： %s\n", cookie)

	c.JSON(http.StatusOK, gin.H{
		"cookie": cookie,
	})
}
func test(c *gin.Context) {
	cookie, _ := c.Cookie("key_cookie")
	c.JSON(200, gin.H{
		"cookie": cookie,
	})
}
